use serde::{Deserialize, Serialize};

use crate::features::users::modelsdbo::UserDBO;
use async_graphql::{Context, ID};
use derivative::Derivative;

#[derive(async_graphql::SimpleObject, Serialize, Deserialize, Clone)]
#[graphql(complex)]
#[derive(Derivative)]
#[derivative(Default)]
pub struct User {
  pub id: ID,
  pub email: String,
  pub username: String,
  pub cred: String,
}

#[async_graphql::ComplexObject]
impl User {
  pub async fn from(&self) -> String {
    let mut from = String::new();
    from.push_str(&self.username);
    from.push_str("<");
    from.push_str(&self.email);
    from.push_str(">");

    from
  }
}

impl From<UserDBO> for User {
  fn from(item: UserDBO) -> Self {
    User {
      id: ID::from(item.id),
      email: item.email,
      username: item.username,
      cred: item.cred,
      ..Default::default()
    }
  }
}


#[derive(async_graphql::InputObject, Serialize, Deserialize, Clone, Debug)]
pub struct NewUser {
  pub email: String,
  pub username: String,
}

#[derive(async_graphql::InputObject, Serialize, Deserialize, Clone, Debug)]
pub struct SignIn {
  username: String,
  password: String,
}
